window.onload = function () {
    findByItem(1);
}

/**
 * 动态条件查询
 * @param pageNo
 */
function findByItem(pageNo) {
    axios.get("/product/findByItem",{
        params:{
            pageNo,
            name:$("nameTxt").value,
            starDate:$("startTxt").value,
            endDate:$("endTxt").value
        }
    }).then(resp=>{
        var info = resp.data;
    //    表格
        var str = "";
        info.list.forEach(n=>{
            str+=`<tr><td>${n.name}</td><td>${n.createDate}</td><td>${n.price}</td><td><a href="/product/update.html?id=${n.id}">修改</a>
<a href="javascript:del(${n.id})">删除</a></td></tr>`;
        });
        $("data").innerHTML=str;
    //    页码
        var pageStr="";
        for (var i=1;i<=info.totalPage;i++){
            pageStr+=`<a href="javascript:findByItem(${i})" style="margin-left: 10px">${i}</a>`;
        }
        $("cutDiv").innerHTML=pageStr;
    })
}

function del(id) {
    axios.get("/product/del",{
        params: {
            id
        }
    }).then(resp=>{
        var info =resp.data;
        if (info=="ok"){
            findByItem(1);
        }
    })
}
function showAdd() {
    $("addDiv").style.visibility="visible";
}
function add() {
    axios.get("/product/add",{
        params: {
            name: $("addName").value,
            createDate: $("addDate").value,
            price:$("addPrice").value
        }
        }).then(resp=>{
            var info = resp.data;
            if (info=="ok"){
                findByItem(1);
                $("addDiv").style.visibility="hidden";
            }
    });
}